<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>系统管理--角色管理</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" href="css/chuchu/chuchu.css" type="text/css"></link>
<link rel="stylesheet" href="easyui/themes/icon.css" type="text/css"></link>
	 <link rel="stylesheet" href="easyui/themes/default/easyui.css" type="text/css"></link>
	 <script type="text/javascript" src="easyui/jquery-1.8.3.js"></script>
	 <script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
  		$(function(){
			$.post(
				"power/findAll.do",
				function(data){
					var str="";
					for(var i=0;i<data.length;i++){
						str+="<input name='"+data[i].id+"' value='"+data[i].id+"' type='checkbox' class='ch1'/>"+data[i].name+"";
						if((i+1)%4==0){
							str+="<br>";
						}
					}
					$("#powername").html(str);
				},
				"json"
			);
			
  			$("#div2").dialog({closed:true});
  			$("#div3").dialog({closed:true});	
  			$("#div4").dialog({closed:true});	
  			$("#div1").datagrid({
  				url:"role/findAll.do",
	  			title:"角色列表",
	  			pagination:true,	//分页
  				pageList:[5,10,15],//可选择每页大小
	  			columns:[[
	  			{
	  			title:"排序",
	  			width:100,
	  			field:"indexs"
	  			},
	  			{
	  			title:"编号",
	  			width:100,
	  			field:"id",
	  			},
	  			{
	  			title:"名称",
	  			width:160,
	  			field:"name"
	  			},
	  			{
	  			title:"操作",
	  			width:160,
	  			field:"x",
	  			formatter:function(value,row){
	  				return "<a onclick='power("+row.id+")' href='javascript:void(0)'>权限设置</a>&nbsp<a onclick='del("+row.id+")' href='javascript:void(0)'>删除</a>&nbsp<a onclick='update("+row.id+")' href='javascript:void(0)'>修改</a>";
	  			}
	  			}
	  			]],
	  			toolbar:[{text:"新增",iconCls:"icon-add",handler:function(){
	  				add();
	  			}}]	
  			});
  		});
  		function date(value){
			return value.year+1900+"-"
			+(value.month+1)+"-"+value.date;
		}
		function add(){
		$("#div2").dialog({
  			title:"新增",
  			width:400,
  			heigeht:200,
  			closed:false,
  			modal:true,
  			buttons:[{text:"确定",
  					handler:function(){
  						var data =  "name="+$("#name").val()+
  						"&indexs="+$("#indexs").val();
  							$.post(
  								"role/add.do",
  								data,
  								function(data){
  									alert(data);
  									$("#div2").dialog("close");
  									$("#div1").datagrid("reload");
  								},
  								"text"
  							);
  					}
  					},
  					{text:"取消",
  					handler:function(){
  							$("#div2").dialog("close");
  						}
  					}]
  		});
  		
		}
		function del(value){
			$.messager.confirm("提示框", "你确定要删除吗?",function(r){
				if(r){
					var data = "id="+value;
						$.post(
			  				"role/delete.do",
			  				data,
			  				function(data){
			  				alert(data);
			  				$("#div1").datagrid("reload");
			  				},
			  				"text"
			  			);
					}
			});
		};
		function update(value){
		 $("#div3").dialog({
  			title:"修改",
  			width:400,
  			heigeht:200,
  			closed:false,
  			modal:true,
  			buttons:[{text:"确定",
  					handler:function(){
  						var data = "id="+value+"&name="+$("#name2").val()+
  						"&indexs="+$("#indexs2").val();
  							$.post(
  								"role/update.do",
  								data,
  								function(data){
  									alert(data);
  									$("#div3").dialog("close");
  									$("#div1").datagrid("reload");
  								},
  								"text"
  							);
  					}
  					},
  					{text:"取消",
  					handler:function(){
  							$("#div3").dialog("close");
  						}
  					}]
  		});
		$.post("role/findById.do","id="+value,function(data){
  				$("#name2").val(data.name);
  				$("#indexs2").val(data.indexs);
  			},"json");
		};
		function power(value){
			$("#div4").dialog({
  			title:"权限管理",
  			width:400,
  			heigeht:200,
  			closed:false,
  			modal:true,
  			buttons:[{text:"保存",
  					handler:function(){
  						var r = $(".ch1");
  						var s = [];
  						var x = [];
  						var j=0;
  						var k=0;
  						for(i=0;i<r.length;i++){
  							if(r[i].checked){
  								s[j]=r[i].name;
  								j++;
  							}else{
  								x[k]=r[i].name;
  								k++;
  							}
  						}
  						var data ="roleid="+value+"&a="+s+"&b="+x;
  							$.post(
  								"rolepower/add.do",
  								data,
  								function(data){
  									alert(data);
  									$("#div4").dialog("close");
  								},
  								"text"
  							);
  					}
  					},
  					{text:"取消",
  					handler:function(){
  							$("#div4").dialog("close");
  						}
  					}]
  		});
  		$.post("role/findById.do","id="+value,function(data){
  				$("#rolename").html(data.name);
  			},"json");
		};
  	</script>

</head>

<body>
	<div id="mainbody">
		<p>
			<img src="css/chuchu/img/u89.png" id="img">&nbsp;&nbsp;<span>系统设置</span>
		</p>
		<div class="title">角色管理</div>
		
			<div id="div1"></div>
			<div id="div2">
				名称：<input type="text" id="name" name="name" /><br> 排序：<input
					type="text" id="indexs" name="indexs" />
			</div>
			<div id="div3">
				名称：<input type="text" id="name2" name="name2" /><br> 排序：<input
					type="text" id="indexs2" name="indexs2" />
			</div>
			<div id="div4">
				角色：<span id="rolename"></span><br><br>
				权限设置：<br><br>
				<div id="powername"></div>
			</div>
	
	</div>
</body>
</html>
